var utils = require('./exports/utils')
var constant = require('./exports/constant')
var point_map = require('./exports/point_map')
var ad_util = require('./exports/ad_util')
var ocrUtils = require('./exports/ocrUtils')
var shop = {
  times: 7,
  go_page_from_home() {},
  init() {},
  start() {
    console.log("shop start");
    if (this.times < 10) {
      this.execute();
    } else {
      this.next();
    }
  },
  execute() {
    var swipe_times = 15 + random(0, 3);
    console.log("即将开始滑动" + swipe_times + "次");
    for (var j = 0; j < swipe_times; j++) {
      swipe(
        device.width / 2,
        (device.height / 10) * 5,
        device.width / 2,
        (device.height / 10) * 2,
        500
      );
      var swipe_after_interval = 3 + random(0, 2);
      console.log(
        "第" +
          (j + 1) +
          "次滑动完成，等待" +
          swipe_after_interval +
          "秒后开始下一次滑动"
      );
      sleep(swipe_after_interval * 1000);
      if (utils.exists_text_starts_with("看视频再得")) {
        console.log("shop execute: 找到--看视频再得--,本次浏览已完成");
        break;
      }
    }
    this.after();
  },
  after() {
    console.log("shop after");
    this.times = this.times + 1;
    if (utils.click_text_starts_with("看视频再得")) {
      console.log("shop after: 找到--看视频再得--");
      sleep(constant.min_click_after_time * 1000);
      sleep(25 * 1000);
      back();
      sleep(2 * 1000);
      if (utils.exists_text_starts_with("恭喜你获得")) {
        console.log("shop after: 找到--恭喜你获得--");
        sleep(constant.min_click_after_time * 1000);
        utils.click_point(488, 1537, 592, 1640);
        sleep(constant.min_click_after_time * 1000);
      }
    }
    console.log("shop after: 已经浏览本页" + this.times + "次");
    if (this.times < 10) {
      var wait_next_interval = 20 + random(0, 3);
      console.log(
        "shop after: 等待" + wait_next_interval + "秒，开始下一次浏览本页"
      );
      sleep(wait_next_interval * 1000);
      this.execute();
      return;
    }
    this.next();
  },
  next() {
    console.log("next");
  },
};

module.exports = shop